package com.treasure.mall.zbqmallmanager.controller.product;

import com.treasure.mall.biz.entity.product.CategoryPO;
import com.treasure.mall.biz.service.core.product.CategoryService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wm
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public String list(Model model) {
        Object a = categoryService.getApiTree();

        return "product/cat_list";
    }

    @PostMapping(value = "/getList")
    @ResponseBody
    public ApiResult getList(Long parentCatId) {
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, categoryService.getList(parentCatId));
    }

    @GetMapping(value = "/getListLevelTop2")
    @ResponseBody
    public List<CategoryPO> getListLevelTop2() {
        return categoryService.getListLevelTop2();
    }

    @PostMapping(value = "/save")
    @ResponseBody
    public ApiResult addOrUpdateCategory(CategoryPO categoryPO) {
        return categoryService.addOrUpdateCategory(categoryPO);
    }

    @PostMapping(value = "/del")
    @ResponseBody
    public ApiResult delCategory(int catId) {
        return categoryService.delCategory(catId);
    }

    @PostMapping(value = "/getSingle")
    @ResponseBody
    public ApiResult getDetail(long catId) {
        CategoryPO po = categoryService.getSingle(catId);
        return po == null ? ApiResult.errorWith(ResultCodeEnum.ERROR, "数据不存在") : ApiResult.resultWith(ResultCodeEnum.SUCCESS, po);
    }


}